package pt.fct.mpc.lastfm.database;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;

public class LastfmDatabaseFavourites
	extends LastfmDatabaseAdapter
{
	public LastfmDatabaseFavourites(Context context)
	{
		super(context);
	}
	
	private void addEntry(String favourite_name, String favourite_type)
	{
		SQLiteDatabase conn = super.getConn();
		
		String[] cols = new String[] { 
			LASTFM_TABLE_FAVOURITES_NAME, 
			LASTFM_TABLE_FAVOURITES_TYPE,
		};
		String select = LASTFM_TABLE_FAVOURITES_NAME+"='"+favourite_name
		+"' and "+LASTFM_TABLE_FAVOURITES_TYPE+"='"+favourite_type+"'";
		
		conn.beginTransaction();
		
		Cursor cursor = conn.query(LASTFM_TABLE_FAVOURITES, 
			cols, select, null, null, null, null);
		
		int row_count = cursor.getCount();
		if (row_count > 1)
			throw new SQLiteException("we have more than one hit on DB");
		
		if (row_count == 0)
		{
			// insert
			ContentValues values = new ContentValues();
			values.put(LASTFM_TABLE_FAVOURITES_NAME, favourite_name);
			values.put(LASTFM_TABLE_FAVOURITES_TYPE, favourite_type);
			
			long insert = conn.insert(LASTFM_TABLE_FAVOURITES, null, values);
			if (insert < 0)
				throw new SQLiteException("unable to insert row");
		}		
		cursor.close();
		
		conn.setTransactionSuccessful();
		conn.endTransaction();
	}
	
	public void removeAll()
	{
		SQLiteDatabase conn = super.getConn();
		
		conn.delete(LASTFM_TABLE_FAVOURITES, null, null);
	}

	public boolean removeEntry(int id)
	{
		SQLiteDatabase conn = super.getConn();
		
		int total = conn.delete(LASTFM_TABLE_FAVOURITES, "_id="+id, null);
		
		return (total >= 0);
	}

	public void addArtist(String artist_name)
	{
		addEntry(artist_name, LASTFM_DATABASE_TYPE_ARTIST);
	}
	
	public void addAlbum(String artist_name, String album_name)
	{
		String favourite_name = artist_name + ":::" + album_name;
		addEntry(favourite_name, LASTFM_DATABASE_TYPE_ALBUM);
	}
	
	public void addEvent(String event_id, String name, String date)
	{
		String favourite_name = event_id + ":::" + name + ":::" + date;
		addEntry(favourite_name, LASTFM_DATABASE_TYPE_EVENT);
	}
	
	public void addEvent(int event_id, String name, String date)
	{
		addEvent(Integer.toString(event_id), name, date);
	}
	
	public FavouritesResult[] getFavourites()
	{
		String[] cols = new String[] { "_id", "name", "type" };
		
		SQLiteDatabase conn = super.getConn();
		
		Cursor cursor =	conn.query(LASTFM_TABLE_FAVOURITES, 
			cols, null, null, null, null, null);
		
		conn = null;
		
		int total = cursor.getCount();
		FavouritesResult[] results = new FavouritesResult[total];
		
		cursor.moveToFirst();
		
		for (int i = 0; 
				(!cursor.isAfterLast()) && (i < total);
				cursor.moveToNext(), i ++)
		{
			int id_index = cursor.getColumnIndexOrThrow("_id");
			int name_index = cursor.getColumnIndexOrThrow("name");
			int type_index = cursor.getColumnIndexOrThrow("type");
			
			int id = cursor.getInt(id_index);
			String name = cursor.getString(name_index);
			String type = cursor.getString(type_index);
			
			results[i] = new FavouritesResult(name, type);
			results[i].setEntryID(id);
		}
		cursor.close();
		
		return results;
	}
	
	public class FavouritesResult
	{
		private int entry_id;
		private String type;
		private String name;

		public FavouritesResult(String name, String type)
		{
			this.name = name;
			this.type = type;
		}
		
		public void setEntryID(int id)
		{
			this.entry_id = id;
		}
		
		public int getEntryID()
		{
			return this.entry_id;
		}

		public String getName() 
		{
			return this.name;
		}
		
		public String getType()
		{
			return this.type;
		}

		public String toStringByType()
		{
			String str;
			String[] fields = name.split(":::");
			
			if (type.equalsIgnoreCase(LASTFM_DATABASE_TYPE_ARTIST))
			{
				str = name;
			} else if (type.equalsIgnoreCase(LASTFM_DATABASE_TYPE_ALBUM))
			{
				str = "Artist: "+ fields[0] + "\n" + "Album: "+fields[1];
			} else if (type.equalsIgnoreCase(LASTFM_DATABASE_TYPE_EVENT))
			{
				str = fields[1] + "\n" + fields[2];
			} else
				str = "Unknown";
			
			return str;
		}
	}
}
